


//
//  ZZFirstVC.m
//  新项目
//
//  Created by 刘耀宗 on 2016/12/21.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "ZZFirstVC.h"
#import "ZZHomeView.h"
#import "ZZCateView.h"
#import "ZZFirstViewModel.h"
#import "ZZHomeModel.h"
#import "ZZHomeZhanBiModel.h"
#import "ZZSettingVC.h"
#import "ZZhomeFirstView.h"
#import "SCChart.h"
#import "ZZDateModel.h"
#import "SCLineChart.h"
#import "ZZHomeSecView.h"
#import "ZZGoodsAnalysisVC.h"
#import "ZZOrderVC.h"
#import "ZZCarVC.h"
#import "ZZInFactoryViewController.h"
@interface ZZFirstVC ()<UIScrollViewDelegate,UIScrollViewDelegate,ZZhomeFirstViewDelegate,ZZhomeViewDelegate,ZZhomeSecViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) ZZHomeView *homeView;
@property (nonatomic, strong) UILabel *fisrstLabel;

@property (nonatomic, strong) NSArray * fillColorArray;

@property (nonatomic, strong) NSArray * imageArray;

@property (nonatomic, strong) NSArray * titleArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) JHHJView *centerView;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, strong) ZZhomeFirstView *firstView;
@property (nonatomic, strong) ZZHomeSecView *secView;
@property (nonatomic, strong) NSArray *besArr;
@property (nonatomic, strong) NSArray *monthArr;

@property (nonatomic, strong) UIScrollView *scrollowView;
@property (nonatomic, strong) UIScrollView *secScrollowView;
@property (nonatomic, strong) ZZHomeModel *model;
@property (nonatomic, assign) BOOL firstFlag;
@property (nonatomic, assign) BOOL secFlag;
@property (nonatomic, copy) NSString *startTime;
@property (nonatomic, copy) NSString *endTime;
@property (nonatomic, strong) SZWaitView *waitV;
@property (nonatomic, copy) NSString *type;//1:金额    2:订单


@end

@implementation ZZFirstVC

-(UIScrollView *)secScrollowView
{
    if (_secScrollowView == nil) {
        _secScrollowView = [[UIScrollView alloc] init];
        _secScrollowView.delegate=self;
    }
    return _secScrollowView;

}
-(UIScrollView *)scrollowView
{
    if (_scrollowView == nil) {
        _scrollowView = [[UIScrollView alloc] init];
        _scrollowView.delegate=self;
    }
    return _scrollowView;
    
}
-(ZZHomeSecView *)secView
{
    if (_secView == nil) {
        _secView = [ZZHomeSecView ViewWithXib];
        _secView.backgroundColor=[UIColor whiteColor];
        _secView.titleLabel.text=@"预约车辆";
        _secView.delegate=self;
        _secView.Secdelegate = self;
        _secView.chartView.flag=1;
    }
    return _secView;

}
-(ZZhomeFirstView *)firstView
{
    if (_firstView == nil) {
        _firstView = [ZZhomeFirstView ViewWithXib];
        _firstView.backgroundColor=[UIColor whiteColor];
        _firstView.delegate=self;
      _firstView.chartView.flag=0;
        
    }
    return _firstView;
    
}
-(JHHJView *)centerView
{
    if (_centerView == nil) {
        _centerView = [JHHJView shareInstanceJHHJViewWithFrame:CGRectMake(0, 0, newwidth, newheight)];
    }
    return _centerView;
    
}
-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];                                                                                                                
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self settitleView];
    [self.view addSubview:self.scrollView];
    self.scrollView.frame=self.view.bounds;
    [self.scrollView sd_addSubviews:@[self.homeView,self.fisrstLabel,self.firstView,self.secView]];
//    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_top_seting"] style:UIBarButtonItemStyleBordered target:self action:@selector(clickForSet)];
//    self.navigationItem.rightBarButtonItem=rightItem;
    
    NSDate *date=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    formatter.dateFormat=@"yyyy-MM-dd";
    NSString *STR=[formatter stringFromDate:date];
    self.endTime=STR;
    
    NSTimeInterval time=24*60*60*31;
    NSDate *date1=[date dateByAddingTimeInterval:-time];
    NSString *STR2=[formatter stringFromDate:date1];
    self.startTime=STR2;
    [self creatWaitViewAction];
    self.type=@"1";
    [self seletedData];
    
    self.scrollView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self seletedData];
    }];

}
//查看订单占比
-(void)lookOrderZB:(ZZHomeView *)view
{
    self.type=@"2";
    //[self.scrollView.mj_header beginRefreshing];
    [self seletedData];
}
    //查看金额占比
-(void)lookMoneyZB:(ZZHomeView *)view
{
    self.type=@"1";
    //[self.scrollView.mj_header beginRefreshing];
    [self seletedData];

}
- (void)creatWaitViewAction {
    // 展示等待页面
    NSString* msg = WAIT_ALERT;
    if(!_waitV){
        _waitV = [[SZWaitView alloc] initWithString:msg];
    }
    CGPoint theCenter = CGPointMake(UIScreen.mainScreen.bounds.size.width/2.0, UIScreen.mainScreen.bounds.size.height/2.0) ;
    _waitV.center = theCenter;
    [_waitV setHidden:NO];
    [[UIApplication sharedApplication].keyWindow addSubview:_waitV];
}

- (void)removeWaitViewAction {
    // 离开等待页面
    _waitV.hidden = YES;
    [_waitV removeFromSuperview];
    _waitV = nil;
}

-(void)clickForSet
{
    //whenPush(ZZSettingVC)
    whenPush(ZZGoodsAnalysisVC)

}

//代理方法时间筛选
-(void)changeTheTimeForSELETECTWithStartTime:(NSString *)startTime andEndTime:(NSString *)endTime
{
    self.startTime=startTime;
    self.endTime=endTime;
    [self.scrollView.mj_header beginRefreshing];
}

//滚动
-(void)scrollGunDong
{
    CGPoint point;
    if (newwidth==320) {
        point=CGPointMake(0, 64+84);
    }else
    {
        point=CGPointMake(0, 64);
    }
    
    [self.scrollView setContentOffset:point animated:YES];
}

-(void)seletedData
{

    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    [[ZZFirstViewModel sharedViewModel] PayTheGoodsWithcompanyid:loginDict[@"CompanyID"] andType:self.type andStarTime:self.startTime andEndTime:self.endTime andSuccess:^(NSDictionary *result) {
        //移除loading
        [self removeWaitViewAction];
        NSString *dicStr=result[@"resultJson"];
        NSDictionary *dic=[WHJSON objectFormJsonString:dicStr];
        ZZHomeModel *model=[ZZHomeModel mj_objectWithKeyValues:dic];
        self.model=model;
        NSString *zhanBiStr=model.Proportion;
        //占比数组
        NSArray *arr=[WHJSON objectFormJsonString:zhanBiStr];
        NSMutableArray *mutableArr=[NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ZZHomeZhanBiModel *model=[ZZHomeZhanBiModel mj_objectWithKeyValues:dic];
            [mutableArr addObject:model];
        }
        if (arr.count==0) {
            self.fisrstLabel.hidden=YES;
        }else
        {
            self.fisrstLabel.hidden=NO;
        }
         //今日新增数据
        self.homeView.todayNewNumLabel.text=[NSString stringWithFormat:@"%@单",model.ToDayAddOrder];
        //今日完成单数
        self.homeView.todayNumLabel.text=[NSString stringWithFormat:@"%@单",model.ToDayOrder];
        //总共完成
        self.homeView.totalComplectLabel.text=[NSString stringWithFormat:@"%@单",model.SumOrderNum];
        model.modelArr=mutableArr;
        self.homeView.allMoneyLabel.text=[NSString stringWithFormat:@"￥ %@",model.MonthTotal];
        NSArray *besArr;
        //第一
        if (self.firstFlag) {
            besArr=[WHJSON objectFormJsonString:model.MonthInFactory];
        }else
        {
        besArr=[WHJSON objectFormJsonString:model.WeekInFactory];
        }
       
        NSMutableArray *besMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in besArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
            [besMutableArr addObject:model];
        }
        self.besArr=besMutableArr;
        NSArray *monthArr;
        //在厂车辆
        if (self.secFlag)
        {
            monthArr=[WHJSON objectFormJsonString:model.MonthBespeak];
        }else
        {
            monthArr=[WHJSON objectFormJsonString:model.WeekBespeak];
        }
        NSMutableArray *monthMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in monthArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
            [monthMutableArr addObject:model];
        }
        self.monthArr=monthMutableArr;
        model.besArr=besMutableArr;
        [ZZUserModel shareViewModel].monthAllMoney=model.MonthTotal;
        float todayMoney=[model.ToDayTotal floatValue];
        self.homeView.todayMoneyLabel.text=[NSString stringWithFormat:@"今日成交总额:￥ %.2lf",todayMoney];
         [ZZUserModel shareViewModel].todayMoney=[NSString stringWithFormat:@"%.2lf",todayMoney];
        self.homeView.todayNumLabel.text=[NSString stringWithFormat:@"%@笔",model.ToDayOrder];
      //  self.homeView.todayNewNumLabel.text=[NSString stringWithFormat:@"%@单",model.YesterDayOrder];
        self.homeView.yesDayComNumLabel.text=[NSString stringWithFormat:@"昨日%@笔",model.ToDayBespeak];
        self.homeView.yesDayNewNumLabel.text=[NSString stringWithFormat:@"昨日%@单",model.YesterBespeak];
      //占比
        __block   NSMutableArray *MUArr=[NSMutableArray array];
        __block    NSMutableArray *titleArr=[NSMutableArray array];
        
        for (ZZHomeZhanBiModel *model in mutableArr) {
            if (model.Type==nil||[model.Type isEqualToString:@"(null)"]||[[model.Type class] isSubclassOfClass:[NSNull class]]) {
                model.Type=@"默认";
            }
            [titleArr addObject:model.Type];
            if (model.ZhanBi==nil||[model.ZhanBi isEqualToString:@"(null)"]||[[model.ZhanBi class] isSubclassOfClass:[NSNull class]]) {
                model.ZhanBi=@"0";
            }
            [MUArr addObject:model.ZhanBi];
        }
        
       for (long int i = MUArr.count - 1; i >= 0; i--) {
            NSString *str=MUArr[i];
            if ([str isEqualToString:@"0"]) {
                [MUArr removeObjectAtIndex:i];
                [titleArr removeObjectAtIndex:i];
            }
        }


        
        if (MUArr.count==0) {
            
        }else
        {
        self.homeView.zhanBiArr=MUArr;
        }
    
            [ZZUserModel shareViewModel].titleArr=titleArr;

        [self prepareUI:titleArr];
        [self.scrollView.mj_header endRefreshing];
    } andFailer:^{
         [self removeWaitViewAction];
        //无网络
        [self.scrollView.mj_header endRefreshing];
             [self.noDataView noNetwithView:self.scrollView andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.scrollView.mj_header beginRefreshing];
        };
        
    }];

}
//点击查看本周
-(void)lookBenZhou:(ZZhomeFirstView *)view
{
    //预约车辆
    if ([view.titleLabel.text isEqualToString:@"预约车辆"]) {
        self.secFlag=NO;
        NSArray *besArr=[WHJSON objectFormJsonString:self.model.WeekBespeak];
        NSMutableArray *besMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in besArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
            [besMutableArr addObject:model];
                NSLog(@"当前的数量是%@",model.Count);
        }

        self.secView.besArr=besMutableArr;
        [self.secView.chartView showInView:self.secView.scrollowView];
        
    }else
    {
        self.firstFlag=NO;
        NSArray *besArr=[WHJSON objectFormJsonString:self.model.WeekInFactory];
        NSMutableArray *besMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in besArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
            [besMutableArr addObject:model];
                NSLog(@"当前的数量是%@",model.Count);
        }
        self.besArr=besMutableArr;
        self.firstView.besArr=self.besArr;
        [self.firstView.chartView showInView:self.firstView.scrollowView];
    }
    
}

//点击查看本月
-(void)lookBenYue:(ZZhomeFirstView *)view
{
    if ([view.titleLabel.text isEqualToString:@"预约车辆"]) {
        self.secFlag=YES;
        
        NSArray *besArr=[WHJSON objectFormJsonString:self.model.MonthBespeak];
        NSMutableArray *besMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in besArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
       
            NSLog(@"当前的数量是%@",model.Count);
            [besMutableArr addObject:model];
        }
 
        self.secView.besArr=besMutableArr;
        [self.secView.chartView showInView:self.secView.scrollowView];
        
    }else
    {
        self.firstFlag=YES;
        NSArray *besArr=[WHJSON objectFormJsonString:self.model.MonthInFactory];
        NSMutableArray *besMutableArr=[NSMutableArray array];
        for (NSDictionary *dic in besArr) {
            ZZDateModel *model=[ZZDateModel mj_objectWithKeyValues:dic];
            [besMutableArr addObject:model];
                NSLog(@"当前的数量是%@",model.Count);
        }
        self.besArr=besMutableArr;
        self.firstView.besArr=self.besArr;
        [self.firstView.chartView showInView:self.firstView.scrollowView];
    }
   

}
-(void)prepareUI:(NSArray *)arr
{
    self.homeView.sd_layout
    .topEqualToView(self.scrollView)
    .rightEqualToView(self.scrollView)
    .leftEqualToView(self.scrollView)
    .heightIs(550);
    
    
    self.fisrstLabel.sd_layout
    .leftSpaceToView(self.scrollView,30)
    .topSpaceToView(self.homeView,10)
    .autoHeightRatio(0);
    [self.fisrstLabel setSingleLineAutoResizeWithMaxWidth:150];
  
    UIView *lastView=self.fisrstLabel;
    
    _fillColorArray=@[CcColor(240, 90, 75),CcColor(27, 166, 225),CcColor(56, 118, 193),CcColor(27, 166, 225)];

    for (int i = 0; i < arr.count; ++i) {
        ZZCateView *view=[ZZCateView ViewWithXib];
        [self.scrollView sd_addSubviews:@[view]];
        view.colorView.backgroundColor=_fillColorArray[i];
        view.titleLabel.text=arr[i];
        view.sd_layout
        .leftSpaceToView(lastView,10)
        .centerYEqualToView(self.fisrstLabel)
        .widthIs(62)
        .heightIs(28);

        lastView=view;
    }
    
    self.firstView.sd_layout
    .topSpaceToView(self.fisrstLabel,30)
    .leftEqualToView(self.scrollView)
    .rightEqualToView(self.scrollView)
    .heightIs(395);
    
     self.firstView.scrollowView.frame=CGRectMake(30, 210, newwidth, 375-210);
    self.secView.sd_layout
    .topSpaceToView(self.firstView,10)
    .leftEqualToView(self.scrollView)
    .rightEqualToView(self.scrollView)
    .heightIs(375);
    
       self.secView.scrollowView.frame=CGRectMake(30, 210, newwidth, 375-210);
    [self.scrollView setupAutoContentSizeWithBottomView:self.secView bottomMargin:20];
    self.firstView.besArr=self.besArr;
    //本周
    self.firstView.carNumLabel.text=[NSString stringWithFormat:@"今日: %@辆",self.model.DayInFactory];
    self.firstView.allNumLabel.text=[NSString stringWithFormat:@"总共: %@辆",self.model.TotalInFactory];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.firstView.carNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r = NSMakeRange(3, self.firstView.carNumLabel.text.length-4);
    NSRange r2 =  NSMakeRange(3, self.firstView.carNumLabel.text.length-4);
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r2];
    [self.firstView.carNumLabel setAttributedText:attributedString];
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:self.firstView.allNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r3 =  NSMakeRange(3, self.firstView.allNumLabel.text.length-4);
    NSRange r4 =  NSMakeRange(3, self.firstView.allNumLabel.text.length-4);
    [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r3];
    [attributedString1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r4];
    [self.firstView.allNumLabel setAttributedText:attributedString1];
        [self.firstView.chartView.lineChart setFlag:0];
    [self.firstView.chartView showInView:self.firstView.scrollowView];
    
    
       self.secView.besArr=self.monthArr;
      [self.secView.chartView.lineChart  setFlag:1];
     [self.secView.chartView showInView:self.secView.scrollowView];
    
    //预约车辆
    self.secView.carNumLabel.text=[NSString stringWithFormat:@"今日: %@辆",self.model.ToDayBespeak];
    self.secView.allNumLabel.text=[NSString stringWithFormat:@"总共: %@辆",self.model.TotalBespeakCar];
    
    NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:self.secView.carNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r5 = NSMakeRange(3, self.secView.carNumLabel.text.length-4);
    NSRange r6 =  NSMakeRange(3, self.secView.carNumLabel.text.length-4);
    
    [attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r5];
    [attributedString2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r6];
    [self.secView.carNumLabel setAttributedText:attributedString];
    
    NSMutableAttributedString *attributedString3 = [[NSMutableAttributedString alloc] initWithString:self.secView.allNumLabel.text]; // 改变特定范围颜色大小要用的
    NSRange r7 = NSMakeRange(3, self.secView.allNumLabel.text.length-4);
    NSRange r8 =  NSMakeRange(3, self.secView.allNumLabel.text.length-4);
    
    [attributedString3 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r7];
    [attributedString3 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0f] range:r8];
    [self.secView.allNumLabel setAttributedText:attributedString3];

    
    
    [self.noDataView dataIsEmpty:arr withView:self.scrollView andNoDataView:self.noDataView andRect:CGRectMake(0, 330, newwidth, 250) andBtnHiddne:YES];
    self.noDataView.label.text=@"暂无信息";
    
}
-(void)settitleView
{
    UILabel *label=[[UILabel alloc] init];
    label.font=newFont(20);
    label.text=@"商家首页";
    label.textColor=[UIColor blackColor];
    [label sizeToFit];
    label.frame=CGRectMake(newwidth/2-label.frame.size.width/2, 20, label.frame.size.width, label.frame.size.height);
    self.navigationItem.titleView=label;
    
}
-(UILabel *)fisrstLabel
{
    if (_fisrstLabel == nil) {
        _fisrstLabel = [[UILabel alloc] init];
        _fisrstLabel.text=@"分类订单统计";
        _fisrstLabel.font=newFont(16);
        _fisrstLabel.textColor=CcColor(102, 102, 102);
        [_fisrstLabel sizeToFit];
    }
    return _fisrstLabel;
    
    
}
-(ZZHomeView *)homeView
{
    if (_homeView == nil) {
        _homeView = [ZZHomeView ViewWithXib];
        _homeView.delegate = self;
        
//        self.homeView=nil;
//        self.homeView=[ZZHomeView ViewWithXib];
//        self.homeView.delegate = self;
        [self.scrollView sd_addSubviews:@[_homeView]];
    }
    return _homeView;
    
}
-(UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate=self;
        _scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    }
    return _scrollView;
    
}

-(void)toPushOrderDetail:(ZZHomeView *)homeView withTag:(NSInteger)tag
{
    ZZOrderVC *vc = [[ZZOrderVC alloc]init];
    if (tag==887) {
        vc.type=@"1";
        
    }else if (tag==888)
    {vc.type=@"2";
    
    }else
    {
    vc.type=@"3";
    }
    PushVC(vc)
}

-(void)toPushOnFactoryDetail:(ZZhomeFirstView *)firstView withTag:(NSInteger)tag
{
    ZZInFactoryViewController *vc = [[ZZInFactoryViewController alloc]init];
    //ZZCarVC *vc =[[ZZCarVC alloc]init];
    //vc.selectMark = 2;
    if (tag == 1011) {
        vc.timeType = @"1";
        vc.type = @"2";
    }else if (tag == 1022){
        vc.timeType = @"2";
        vc.type = @"2";
    }
    PushVC(vc)
}

-(void)toPushOrderCarDetail:(ZZhomeFirstView *)firstView withTag:(NSInteger)tag
{
    ZZInFactoryViewController *vc = [[ZZInFactoryViewController alloc]init];
  
    if (tag == 1888) {
        vc.timeType = @"1";
        vc.type= @"1";
    }else if (tag == 1999){
        vc.timeType = @"2";
         vc.type= @"1";
    }
    PushVC(vc)
}
@end
